package cn.tedu.luban.worker.main.repository.impl;

import cn.tedu.luban.worker.common.enums.StatusEnum;
import cn.tedu.luban.worker.main.converter.WorkerConverter;
import cn.tedu.luban.worker.main.dao.WorkerDAO;
import cn.tedu.luban.worker.main.repository.WorkerRepository;
import cn.tedu.luban.worker.protocol.bo.AdminWorkerBO;
import cn.tedu.luban.worker.protocol.bo.FrontWorkerBO;
import cn.tedu.luban.worker.protocol.dos.WorkerDO;
import cn.tedu.luban.worker.protocol.param.WorkerRegistParam;
import cn.tedu.luban.worker.protocol.param.WorkerParam;
import cn.tedu.luban.worker.protocol.query.WorkerDBPageQuery;
import cn.tedu.luban.worker.protocol.query.WorkerListQuery;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class WorkerRepositoryImpl implements WorkerRepository {
    @Autowired(required = false)
    private WorkerDAO workerDAO;
    @Autowired
    private WorkerConverter workerConverter;
    @Override public void delete(Long userId) {
        workerDAO.deleteByUserId(userId);
    }

    @Override public Long saveWorker(WorkerRegistParam param) {
        WorkerDO workerDO=workerConverter.param2do(param);
        workerDAO.insert(workerDO);
        return workerDO.getId();
    }

    @Override public Long countWorker(WorkerListQuery query) {
        WorkerDBPageQuery dbQuery=workerConverter.query2dbQuery(query);
        return workerDAO.countWorker(dbQuery);
    }

    @Override public void updateCertStatus(WorkerParam param) {
        WorkerDO workerDO=workerConverter.param2do(param);
        workerDAO.updateCertStatus(workerDO);
    }

    @Override public List<AdminWorkerBO> queryWorkers(WorkerListQuery query) {
        WorkerDBPageQuery dbQuery=workerConverter.query2dbQuery(query);
        List<WorkerDO> workerDOS=workerDAO.selectWorkers(dbQuery);
        return workerConverter.dos2bos(workerDOS);
    }

    @Override public AdminWorkerBO getWorker(Long userId) {
        WorkerDO workerDO = workerDAO.selectByUserId(userId);
        return workerConverter.do2bo(workerDO);
    }

    @Override public void updateAuditStatus(WorkerParam param) {
        WorkerDO workerDO=workerConverter.param2do(param);
        workerDAO.updateAuditStatus(workerDO);
    }

    @Override public Integer enable(Long id) {
        WorkerParam workerParam=new WorkerParam();
        workerParam.setStatus(StatusEnum.NORMAL.getValue());
        workerParam.setId(id);
        WorkerDO workerDO = workerConverter.param2do(workerParam);
        return workerDAO.updateStatus(workerDO);
    }

    @Override public Integer disable(Long id) {
        WorkerParam workerParam=new WorkerParam();
        workerParam.setStatus(StatusEnum.LOCKED.getValue());
        workerParam.setId(id);
        WorkerDO workerDO = workerConverter.param2do(workerParam);
        return workerDAO.updateStatus(workerDO);
    }

    @Override public FrontWorkerBO getFrontWorker(Long userId) {
        WorkerDO workerDO=workerDAO.selectByUserId(userId);
        return workerConverter.do2frontBo(workerDO);
    }
}
